package com.dsg.controller.account;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.account.DsgAccount;
import com.dsg.service.account.IDsgAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 油公司總帳 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-12
 */
@RestController
@RequestMapping("/account/dsgAccount")
@Api(description = "油公司總帳")
public class DsgAccountController extends BaseController {

    @Autowired
    private IDsgAccountService dsgAccountService;

    /**
     * 查询列表汇总（分页）
     *
     * @param dsgAccount
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表汇总（分页）")
    public AjaxResult list(DsgAccount dsgAccount) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        if (StringUtil.isEmpty(sort)) {
            orderItemList.add(new OrderItem().setColumn("oilCom").setAsc(false));
            orderItemList.add(new OrderItem().setColumn("accountNo").setAsc(false));
            page.setOrders(orderItemList);
        }
//        dsgAccount.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = dsgAccountService.selectByList(page, dsgAccount);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 查询单条数据
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}/view")
    @ApiOperation("查询单条数据")
    public AjaxResult view(@PathVariable String id) {
        DsgAccount dsgAccount = dsgAccountService.getById(id);
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgAccount);
        return ajaxResult;
    }

    /**
     * 查詢列表明細（分頁）
     *
     * @param dsgAccount
     * @return
     */
    @GetMapping(value = "/listDetailed")
    @ApiOperation("查詢列表明細（分頁）")
    public AjaxResult listDetailed(DsgAccount dsgAccount) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        if (StringUtil.isEmpty(sort)) {
            if (dsgAccount.getIsCash().intValue() == DsgAccount.IS_CASH_0) {
                orderItemList.add(new OrderItem().setColumn("CREATE_TIME").setAsc(false));
            } else {
                orderItemList.add(new OrderItem().setColumn("CASH_TIME").setAsc(false));
            }
            page.setOrders(orderItemList);
        }
        dsgAccount.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = dsgAccountService.selectByListDetailed(page, dsgAccount);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 新增
     *
     * @param dsgAccount
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgAccount dsgAccount) {
        boolean flag = dsgAccountService.addDsgAccount(dsgAccount);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 更新
     *
     * @param dsgAccount
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgAccount dsgAccount) {
        AjaxResult ajaxResult;
        boolean flag = dsgAccountService.updateById(dsgAccount);
        if (flag) {
            ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgAccount);
        } else {
            ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("批量删除")
    public AjaxResult delete(@ApiParam(value = "id以，号分隔", name = "idList", required = true) @RequestParam String idList) {
        Boolean flag = dsgAccountService.removeByIds(Arrays.asList(idList.split(",")));
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 兌現
     *
     * @return
     */
    @PutMapping(value = "/cash")
    @ApiOperation("兌現")
    public AjaxResult cash(@ApiParam(value = "id以，号分隔", name = "idList", required = true) @RequestParam String idList) {
        dsgAccountService.cash(idList);
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }

    /**
     * 更新時否計入統計
     *
     * @return
     */
    @PutMapping(value = "/updateDsgAccountIsInSta")
    @ApiOperation("更新時否計入統計")
    public AjaxResult updateDsgAccountIsInSta(@ApiParam(value = "主鍵", name = "oid", required = true) @RequestParam String oid,
                                              @ApiParam(value = "昰否計入統計", name = "isInSta", required = true) @RequestParam Integer isInSta) {
        DsgAccount dsgAccount = dsgAccountService.getById(oid);
        if (dsgAccount != null) {
            dsgAccount.setIsInSta(isInSta);
            dsgAccountService.updateById(dsgAccount);
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return json(MessageConstants.SSO_STATUS_FAIL,"未查询到数据!");
        }
    }
}
